export function renderMixin(Vue) {
    Vue.prototype._render = function () { //render函数变为vnode
        let vm = this
        let render = vm.$options.render
        let vnode = render.call(vm)
        
       return vnode
    }

    // 处理render中的标签
    Vue.prototype._c = function () {
        return createElement(this,...arguments)
    }
    //  处理render的文本
    Vue.prototype._v = function (text) {
        return createText(this,text)
    }
    // 处理render的变量
    Vue.prototype._s = function (val) {
       return val == null ? "" :(typeof val === 'object' ?JSON.stringify(val):val)
    }
}

// 创建虚拟元素的方法
export function createElement(vm,tag,data={},...children) {
    // 判断是标签还是自定义组件
    if(isResved(tag)){
        // 是原生的标签
        return vnode(vm,tag,data,data.key,children)
    } else {
        // 是自定义的组件
        const Ctor = vm.$options['components'][tag]
        return createComponent(vm,tag,data,children,Ctor)
        
    }
    
}
// 创建自定义组件的虚拟dom
function createComponent(vm,tag,data,children,Ctor) {
    if(typeof Ctor == 'object' || typeof Ctor == 'funciton' ){
        Ctor = vm.constructor.extend(Ctor)
    }
    data.hook = {
        init(vnode){
            // 返回子类
            let child  = vm.componentInstance = new vnode.componentOptions.Ctor({})
            // child.$mount('#btn')
            
        }
    }
    return vnode(vm,'vue-component-'+tag,data,undefined,undefined,undefined,{Ctor,children})
}

// 判断是原生标签还是自定义组件标签
function isResved(tag) {
    const htmlTag = 'html,body,base,head,link,meta,style,title,address,article,aside,footer,header,h1,h2,h3,h4,h5,h6,hgroup,nav,section,div,dd,dl,dt,figcaption,figure,picture,hr,img,li,main,ol,p,pre,ul,a,b,abbr,bdi,bdo,br,cite,code,data,dfn,em,i,kbd,mark,q,rp,rt,rtc,ruby,s,samp,small,span,strong,sub,sup,time,u,var,wbr,area,audio,map,track,video,embed,object,param,source,canvas,script,noscript,del,ins,caption,col,colgroup,table,thead,tbody,td,th,tr,button,datalist,fieldset,form,input,label,legend,meter,optgroup,option,output,progress,select,textarea,details,dialog,menu,menuitem,summary,content,element,shadow,template,blockquote,iframe,tfoot'.split(',')
    return htmlTag.includes(tag)
}

// 创建虚拟文本
function createText(vm,text) {
    return vnode(vm,undefined,undefined,undefined,undefined,text)
}

// 创建虚拟节点
function vnode(vm,tag,data,key,children,text,componentOptions) {
    return {
        vm,
        tag,
        data,
        key,
        children,
        text,
        componentOptions
    }
}